package com.hxkcloud.nurse.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.activitys.ChartDetailActivity;
import com.hxkcloud.nurse.activitys.MessageMainActivity;
import com.hxkcloud.nurse.activitys.MyReportExpandListActivity;
import com.hxkcloud.nurse.activitys.TakeDrugsActivity;
import com.hxkcloud.nurse.beans.MonitorKindBean;
import com.hxkcloud.nurse.dborm.MonitorKindDao;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.hxkcloud.nurse.utils.StringUtil;

import java.util.List;

/**
 * Created by william_sun on 2016-11-18.
 * Description:患者端蜂窝状的监测模块
 */
public class MonitorPatientFragment extends BaseFragment implements View.OnClickListener {
    private View view;
    private LinearLayout ll_title;
    private TextView tv_title;
    private ImageView iv_message;
    private ImageView iv_msgpoint;
    private ImageView iv_logo;
    private LinearLayout ll_back;
    private View view_xueya;
    private View view_xuetang;
    private View view_xuezhi;
    private View view_drug;
    private View view_report;

    private List<MonitorKindBean> kinds;
    private MonitorKindBean hasXueya = null;
    private MonitorKindBean hasXuetang = null;
    private MonitorKindBean hasXuezhi = null;

    @Override
    protected void updateByHandler(Message msg) {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_monitorpatient, null);

        MonitorKindDao monitorKindDao = new MonitorKindDao(mContext);
        kinds = monitorKindDao.getKinds();
        for (MonitorKindBean kind : kinds) {
            if (kind.getName().contains("血压")) {
                hasXueya = kind;
            }
            if (kind.getName().contains("血糖")) {
                hasXuetang = kind;
            }
            if (kind.getName().contains("血脂")) {
                hasXuezhi = kind;
            }
        }

        initView();
        return view;
    }

    public void isMonitorReady() {
        MonitorKindDao monitorKindDao = new MonitorKindDao(mContext);
        kinds = monitorKindDao.getKinds();
        for (MonitorKindBean kind : kinds) {
            if (kind.getName().contains("血压")) {
                hasXueya = kind;
            }
            if (kind.getName().contains("血糖")) {
                hasXuetang = kind;
            }
            if (kind.getName().contains("血脂")) {
                hasXuezhi = kind;
            }
        }
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) view.findViewById(R.id.ll_title);
        tv_title = (TextView) view.findViewById(R.id.tv_title_title);
        iv_message = (ImageView) view.findViewById(R.id.iv_title_func);
        iv_msgpoint = (ImageView) view.findViewById(R.id.iv_title_point);

        setTitlePadding(ll_title);
        tv_title.setText(getResources().getText(R.string.medical_record));
        iv_message.setVisibility(View.VISIBLE);
        iv_message.setImageResource(R.mipmap.nav_message);
        iv_message.setOnClickListener(this);

        iv_logo = (ImageView) view.findViewById(R.id.iv_monitorpatient_logo);
        ll_back = (LinearLayout) view.findViewById(R.id.ll_monitorpatient_back);
        view_xueya = view.findViewById(R.id.view_monitorpatient_xueya);
        view_xuetang = view.findViewById(R.id.view_monitorpatient_xuetang);
        view_xuezhi = view.findViewById(R.id.view_monitorpatient_xuezhi);
        view_drug = view.findViewById(R.id.view_monitorpatient_drug);
        view_report = view.findViewById(R.id.view_monitorpatient_report);

        LinearLayout.LayoutParams lp_logo = new LinearLayout.LayoutParams(screenWidth / 3, screenWidth / 4);
        iv_logo.setLayoutParams(lp_logo);
        int topMargin = ImageUtil.dip2px(20, mContext);
        int leftMargin = ImageUtil.dip2px(12, mContext);
        LinearLayout.LayoutParams lp_back = new LinearLayout.LayoutParams(screenWidth - leftMargin - leftMargin, (screenWidth - leftMargin - leftMargin) * 2 / 3);
        lp_back.setMargins(leftMargin, topMargin, leftMargin, topMargin);
        ll_back.setLayoutParams(lp_back);

        view_xueya.setOnClickListener(this);
        view_xuetang.setOnClickListener(this);
        view_xuezhi.setOnClickListener(this);
        view_drug.setOnClickListener(this);
        view_report.setOnClickListener(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();

        if (YjhApplication.msg_new) {
            iv_msgpoint.setVisibility(View.VISIBLE);
        } else {
            iv_msgpoint.setVisibility(View.INVISIBLE);
        }
    }

    public void showPoint(boolean show) {
        if (iv_msgpoint != null)
            if (show) {
                iv_msgpoint.setVisibility(View.VISIBLE);
            } else {
                iv_msgpoint.setVisibility(View.INVISIBLE);
            }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_func:
                if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                    Intent iM = new Intent(mContext, MessageMainActivity.class);
                    startActivity(iM);
                } else {
                    toastMsg(R.string.pleasefirstjoindepartment);
                }
                break;
            case R.id.view_monitorpatient_xueya:
                if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                    if (hasXueya != null) {
                        StringUtil.zhugeTrack(mContext, "血压" + "录入保存按钮", "血压" + "录入保存按钮点击数");

                        Intent intent = new Intent(mContext, ChartDetailActivity.class);
                        intent.putExtra("patientId", loginInfo.getPatientId());
                        intent.putExtra("kindId", hasXueya.getKindId());
                        intent.putExtra("name", hasXueya.getName());
                        intent.putExtra("min", hasXueya.getMinValue());
                        intent.putExtra("max", hasXueya.getMaxValue());
                        if (hasXueya.getValueList() != null && hasXueya.getValueList().size() > 0) {
                            for (int i = 0; i < hasXueya.getValueList().size(); i++) {
                                intent.putExtra("text" + i, hasXueya.getValueList().get(i).getName());
                            }
                        } else {
                            intent.putExtra("text" + 0, hasXueya.getName());
                        }
                        startActivity(intent);
                    } else {
                        toastMsg(R.string.departmenthavenotthismonitor);
                    }
                } else {
                    toastMsg(R.string.pleasefirstjoindepartment);
                }
                break;
            case R.id.view_monitorpatient_xuetang:
                if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                    if (hasXuetang != null) {
                        StringUtil.zhugeTrack(mContext, "血糖" + "录入保存按钮", "血糖" + "录入保存按钮点击数");

                        Intent intent = new Intent(mContext, ChartDetailActivity.class);
                        intent.putExtra("patientId", loginInfo.getPatientId());
                        intent.putExtra("kindId", hasXuetang.getKindId());
                        intent.putExtra("name", hasXuetang.getName());
                        intent.putExtra("min", hasXuetang.getMinValue());
                        intent.putExtra("max", hasXuetang.getMaxValue());
                        if (hasXuetang.getValueList() != null && hasXuetang.getValueList().size() > 0) {
                            for (int i = 0; i < hasXuetang.getValueList().size(); i++) {
                                intent.putExtra("text" + i, hasXuetang.getValueList().get(i).getName());
                            }
                        } else {
                            intent.putExtra("text" + 0, hasXuetang.getName());
                        }
                        startActivity(intent);
                    } else {
                        toastMsg(R.string.departmenthavenotthismonitor);
                    }
                } else {
                    toastMsg(R.string.pleasefirstjoindepartment);
                }
                break;
            case R.id.view_monitorpatient_xuezhi:
                if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                    if (hasXuezhi != null) {
                        StringUtil.zhugeTrack(mContext, "血脂" + "录入保存按钮", "血脂" + "录入保存按钮点击数");

                        Intent intent = new Intent(mContext, ChartDetailActivity.class);
                        intent.putExtra("patientId", loginInfo.getPatientId());
                        intent.putExtra("kindId", hasXuezhi.getKindId());
                        intent.putExtra("name", hasXuezhi.getName());
                        intent.putExtra("min", hasXuezhi.getMinValue());
                        intent.putExtra("max", hasXuezhi.getMaxValue());
                        if (hasXuezhi.getValueList() != null && hasXuezhi.getValueList().size() > 0) {
                            for (int i = 0; i < hasXuezhi.getValueList().size(); i++) {
                                intent.putExtra("text" + i, hasXuezhi.getValueList().get(i).getName());
                            }
                        } else {
                            intent.putExtra("text" + 0, hasXuezhi.getName());
                        }
                        startActivity(intent);
                    } else {
                        toastMsg(R.string.departmenthavenotthismonitor);
                    }
                } else {
                    toastMsg(R.string.pleasefirstjoindepartment);
                }
                break;
            case R.id.view_monitorpatient_drug:
                Intent drugIntent = new Intent(getActivity(), TakeDrugsActivity.class);
                startActivity(drugIntent);
                break;
            case R.id.view_monitorpatient_report:
                Intent certIntent = new Intent(getActivity(), MyReportExpandListActivity.class);
                startActivity(certIntent);
                break;
        }
    }
}
